package services

import (
	"tencent-video-desktop/backend/internal/models"
)

type VideoService struct {
	videos []models.Video
}

func NewVideoService() *VideoService {
	return &VideoService{
		videos: []models.Video{
			{ID: 1, Title: "庆余年 第二季 第1集", Description: "范闲归来，再掀风云", CoverURL: "/covers/1.jpg", VideoURL: "/videos/1.mp4", Duration: 2730, Category: "电视剧", Tags: "古装,剧情", ViewCount: 1200000},
			{ID: 2, Title: "繁花 第1集", Description: "王家卫导演，胡歌主演", CoverURL: "/covers/2.jpg", VideoURL: "/videos/2.mp4", Duration: 3120, Category: "电视剧", Tags: "都市,剧情", ViewCount: 850000},
			{ID: 3, Title: "三体 第1集", Description: "刘慈欣科幻巨作改编", CoverURL: "/covers/3.jpg", VideoURL: "/videos/3.mp4", Duration: 2915, Category: "电视剧", Tags: "科幻,剧情", ViewCount: 530000},
		},
	}
}

func (s *VideoService) GetVideos() []models.Video {
	return s.videos
}

func (s *VideoService) GetVideoByID(id uint) *models.Video {
	for _, video := range s.videos {
		if video.ID == id {
			return &video
		}
	}
	return nil
}

func (s *VideoService) SearchVideos(keyword string) []models.Video {
	var results []models.Video
	for _, video := range s.videos {
		if containsKeyword(video.Title, keyword) || containsKeyword(video.Description, keyword) {
			results = append(results, video)
		}
	}
	return results
}

func containsKeyword(text, keyword string) bool {
	// 简单的字符串包含检查
	// 实际应用中可以使用更复杂的搜索算法
	for i := 0; i < len(text)-len(keyword)+1; i++ {
		if text[i:i+len(keyword)] == keyword {
			return true
		}
	}
	return false
}

func (s *VideoService) GetVideosByCategory(category string) []models.Video {
	var results []models.Video
	for _, video := range s.videos {
		if video.Category == category {
			results = append(results, video)
		}
	}
	return results
}
